package com.egao.common.manage.school;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.egao.common.component.elasticsearch.entity.vo.StudentPushMsgVO;
import com.egao.common.component.elasticsearch.service.ElasticSearchService;
import com.egao.common.core.annotation.ApiPageParam;
import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.exception.BusinessException;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.manage.TableStatistics;
import com.egao.common.module.company.constants.CompanyConstants;
import com.egao.common.module.company.entity.Company;
import com.egao.common.module.student.constants.StudentConstants;
import com.egao.common.module.student.entity.Student;
import com.egao.common.module.student.entity.StudentRecord;
import com.egao.common.module.student.entity.vo.StudentExcelVO;
import com.egao.common.module.student.entity.vo.StudentFormVO;
import com.egao.common.module.student.entity.vo.StudentListVO;
import com.egao.common.module.student.service.StudentIntenService;
import com.egao.common.module.student.service.StudentRecordService;
import com.egao.common.module.student.service.StudentService;
import com.egao.common.module.system.constants.DictConstants;
import com.egao.common.module.system.constants.UserConstants;
import com.egao.common.module.system.entity.College;
import com.egao.common.module.system.entity.Dict;
import com.egao.common.module.system.entity.QsSchool;
import com.egao.common.module.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 校生源管理
 *
 * @author lym
 */
@Api(tags = "校生源管理")
@RestController
@RequestMapping("${api}")
public class SchoolStudentController extends BaseController {

    @Autowired
    private StudentService studentService;
    @Autowired
    private CollegeService collegeService;
    @Autowired
    private MajorService majorService;
    @Autowired
    private StudentRecordService studentRecordService;
    @Autowired
    private DictService dictService;
    @Autowired
    private YearService yearService;
    @Autowired
    private ElasticSearchService elasticSearchService;
    @Autowired
    private StudentIntenService studentIntenService;
    @Autowired
    private QsSchoolService qsSchoolService;

    private final String title = "校生源管理";

    @PreAuthorize("hasAuthority('school:student:list')")
    @OperLog(value = title, desc = "分页查询生源")
    @ApiOperation(value = "分页查询生源", notes = "学生提交状态倒序 学号升序")
    @ApiPageParam
    @GetMapping("/school/student/page")
    public PageResult<StudentListVO> page(HttpServletRequest request) {
        PageParam<StudentListVO> pageParam = new PageParam<>(request);

        String degree = null;
        degree = UserConstants.DEGREE_SCOPE_ALL.equals(getLoginUser().getDegreeScope()) ? null : getLoginUser().getDegreeScope();
        if (StrUtil.isBlank(pageParam.getString("degree"))) {
            pageParam.put("degree", degree);
        }

        pageParam.setOrder("student_state", false)
                .setOrder("xh", true);

        return new PageResult<>(studentService.listPage(pageParam), pageParam.getTotal());
    }

    @PreAuthorize("hasAuthority('school:student:list')")
    @ApiOperation(value = "查询所有学院")
    @GetMapping("/school/student/colleges")
    public JsonResult colleges() {
        return JsonResult.ok().setData(collegeService.listAll(null, ""));
    }

    @PreAuthorize("hasAuthority('school:student:list')")
    @ApiOperation(value = "查询所有专业")
    @GetMapping("/school/student/majors")
    public JsonResult majors() {
        return JsonResult.ok().setData(majorService.listAll("", null));
    }

    @PreAuthorize("hasAuthority('school:student:list')")
    @ApiOperation(value = "查询列表字典")
    @GetMapping("/school/student/dicts")
    public JsonResult dicts() {
        Map<String, List<Dict>> map = new HashMap<>();
        map.put("pyfsList", dictService.cacheList(DictConstants.TYPE_PYFS));
        map.put("syszdList", dictService.cacheList(DictConstants.TYPE_SYSZD));
        return JsonResult.ok().setData(map);
    }

    @PreAuthorize("hasAuthority('school:student:list')")
    @ApiOperation(value = "查询表单字典")
    @GetMapping("/school/student/form-dict")
    public JsonResult formDicts() {
        String[] typeArr = {
                DictConstants.TYPE_PYFS,
                DictConstants.TYPE_SYSZD,
                DictConstants.TYPE_ZXJH,
                DictConstants.TYPE_KNSLB,
                DictConstants.TYPE_ZZMM,
                DictConstants.TYPE_MZ,
                DictConstants.TYPE_ZXWYYZ,
                DictConstants.TYPE_YXDWXZ,
                DictConstants.TYPE_YXDWHY,
                DictConstants.TYPE_YXZWLB,
                DictConstants.TYPE_YXYX,
                DictConstants.COMPANY_NATURE,
                DictConstants.COMPANY_INDUSTRY
        };
        return JsonResult.ok().setData(dictService.cacheMaps(typeArr));
    }

    @PreAuthorize("hasAuthority('school:student:list')")
    @ApiOperation(value = "查询所有毕业年份")
    @GetMapping("/school/student/year")
    public JsonResult year() {
        return yearService.listAll();
    }

    @PreAuthorize("hasAuthority('school:student:detail')")
    @OperLog(value = title, desc = "学生详情信息")
    @ApiOperation(value = "学生详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "学生ID", required = true, dataType = "int")
    })
    @GetMapping("/school/student/detail")
    public JsonResult student(Integer id) {
        return JsonResult.ok().setData(studentService.getStudentFormVO(id));
    }

    @PreAuthorize("hasAuthority('school:student:detail')")
    @ApiOperation(value = "学生信息记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "类型", required = false, dataType = "string"),
            @ApiImplicitParam(name = "studentId", value = "学生ID", required = true, dataType = "int")
    })
    @GetMapping("/school/student/record")
    public PageResult record(HttpServletRequest request) {
        PageParam<StudentRecord> pageParam = new PageParam<>(request);
        return new PageResult<>(studentRecordService.listPage(pageParam), pageParam.getTotal());
    }

    @PreAuthorize("hasAuthority('school:student:update')")
    @OperLog(value = title, desc = "验证生源重复")
    @ApiOperation(value = "验证生源重复")
    @GetMapping("/school/student/verify-repeat")
    public JsonResult verifyRepeat(String xh) {
        return studentService.verifyRepeat(xh);
    }

    @PreAuthorize("hasAuthority('school:student:update')")
    @OperLog(value = title, desc = "编辑学生信息", result = true)
    @ApiOperation(value = "编辑学生信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "studentFormVO", value = "StudentFormVO", required = true, dataType = "StudentFormVO")
    })
    @PutMapping("/school/student/update")
    public JsonResult update(@RequestBody StudentFormVO studentFormVO) {
        return studentService.schoolSubmit(studentFormVO, getLoginUserId());
    }

    @PreAuthorize("hasAuthority('school:student:delay')")
    @OperLog(value = title, desc = "延期")
    @ApiOperation(value = "延期")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "studentFormVO", value = "StudentFormVO", required = true, dataType = "StudentFormVO")
    })
    @PutMapping("/school/student/delay")
    public JsonResult delay(@RequestBody StudentFormVO studentFormVO) {
        return studentService.delay(studentFormVO, getLoginUserId());
    }

    @PreAuthorize("hasAuthority('school:student:unlock')")
    @OperLog(value = title, desc = "定向生解锁锁定")
    @ApiOperation(value = "定向生解锁锁定")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "studentFormVO", value = "StudentFormVO", required = true, dataType = "StudentFormVO")
    })
    @PutMapping("/school/student/unlock")
    public JsonResult unlock(@RequestBody StudentFormVO studentFormVO) {
        return studentService.unlock(studentFormVO, getLoginUserId());
    }

    @PreAuthorize("hasAuthority('school:student:export')")
    @OperLog(value = title, desc = "导出", result = true)
    @ApiOperation(value = "导出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "导出类型 text文本 code代码", required = true, dataType = "string"),
            @ApiImplicitParam(name = "StudentExcelVO", value = "StudentExcelVO", required = false, dataType = "StudentExcelVO")
    })
    @GetMapping("/school/student/export")
    public void export(HttpServletRequest request, HttpServletResponse response) {
        PageParam<StudentExcelVO> pageParam = new PageParam<>(request);
        studentService.exportStudent(pageParam, response);
    }

    @PreAuthorize("hasAuthority('school:student:delete')")
    @OperLog(value = title, desc = "删除生源")
    @ApiOperation(value = "删除生源")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "学生id逗号相隔字符串", required = true, dataType = "int")
    })
    @GetMapping("/school/student/delete")
    public JsonResult delete(String ids) {
        return studentService.deleteStudent(ids, getLoginUserId());
    }

    @PreAuthorize("hasAuthority('school:student:list')")
    @OperLog(value = title, desc = "查询学生接收推送信息")
    @ApiOperation(value = "查询学生接收推送信息", notes = "查询学生接收推送信息")
    @ApiPageParam
    @GetMapping("/school/student/page-push")
    public PageResult<StudentPushMsgVO> pagePush(HttpServletRequest request) {
        PageParam<StudentListVO> pageParam = new PageParam<>(request);
        try {
            int page = (int) pageParam.getCurrent();
            int limit = (int) pageParam.getSize();
            return elasticSearchService.searchStudentPushMsg(pageParam.getString("id"), page, limit);
        } catch (IOException e) {
            throw new BusinessException("查询异常");
        }
    }

    @PreAuthorize("hasAuthority('school:student:list')")
    @OperLog(value = title, desc = "查看学生就业意向详情")
    @ApiOperation(value = "查看学生就业意向详情")
    @GetMapping("/school/inten/detail")
    public JsonResult detail(Integer id) {
        Student student = studentService.getById(id);
        if (student != null) {
            return JsonResult.ok().setData(studentService.getStudentInten(student.getUserId()));
        }
        return JsonResult.error("学生信息异常");
    }

    @PreAuthorize("hasAuthority('school:student:list')")
    @ApiOperation(value = "学生就业意向详情字典")
    @GetMapping("/school/inten/dict")
    public JsonResult dictMap() {
        return JsonResult.ok().setData(studentService.dictMap());
    }

    @PreAuthorize("hasAuthority('school:student:update')")
    @OperLog(value = title, desc = "开关系统单位自由录入")
    @ApiOperation(value = "开关系统单位自由录入")
    @PutMapping("/school/student/sign-entry/{id}/{state}")
    public JsonResult signEntry(@PathVariable("id") Integer id, @PathVariable("state") String state) {
        studentService.changeSignEntry(studentService.getById(id), getLoginUserId(), state);
        return JsonResult.ok();
    }

    @PreAuthorize("hasAuthority('school:student:update')")
    @OperLog(value = title, desc = "开关学生自由解约")
    @ApiOperation(value = "开关学生自由解约")
    @PutMapping("/school/student/free-termination/{id}")
    public JsonResult freeTermination(@PathVariable("id") Integer id) {
        studentService.updateFreeTermination(id, getLoginUserId());
        return JsonResult.ok();
    }

    @PreAuthorize("hasAuthority('school:student:export')")
    @OperLog(value = title, desc = "导出求职意向", result = true)
    @ApiOperation(value = "导出求职意向")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "StudentExcelVO", value = "StudentExcelVO", required = false, dataType = "StudentExcelVO")
    })
    @GetMapping("/school/student/export-job")
    public void exportJob(HttpServletRequest request, HttpServletResponse response) {
        PageParam<StudentExcelVO> pageParam = new PageParam<>(request);
        studentService.exportJob(pageParam, response);
    }

    @PreAuthorize("hasAuthority('school:student:export')")
    @OperLog(value = title, desc = "导出毕业去向意向", result = true)
    @ApiOperation(value = "导出毕业去向意向")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "StudentExcelVO", value = "StudentExcelVO", required = false, dataType = "StudentExcelVO")
    })
    @GetMapping("/school/student/export-gone")
    public void exportGone(HttpServletRequest request, HttpServletResponse response) {
        PageParam<StudentExcelVO> pageParam = new PageParam<>(request);
        studentService.exportGone(pageParam, response);
    }

    @PreAuthorize("hasAuthority('school:student:list')")
    @ApiOperation(value = "列表统计")
    @GetMapping("/school/student/statistics")
    public JsonResult statistics(Integer yearId) {
        List<Student> list = studentService.list(new LambdaQueryWrapper<Student>().eq(Student::getYearId, yearId));

        long bk = list.stream().filter(d -> StudentConstants.STUDENT_XL_BK.equals(d.getXl())).count();
        long bkrxw = list.stream().filter(d -> StudentConstants.STUDENT_XL_BKRXW.equals(d.getXl())).count();
        long ss = list.stream().filter(d -> StudentConstants.STUDENT_XL_SS.equals(d.getXl())).count();
        long bs = list.stream().filter(d -> StudentConstants.STUDENT_XL_BS.equals(d.getXl())).count();
        long dx = list.stream().filter(d -> StudentConstants.PYFS_DX.equals(d.getPyfs())).count();
        long wp = list.stream().filter(d -> StudentConstants.PYFS_WP.equals(d.getPyfs())).count();

        long kns = list.stream().filter(d -> d.getKnslb() != null && !StrUtil.isBlank(d.getKnslb()) && !"0".equals(d.getKnslb())).count();
        long jzbfdx = list.stream().filter(d -> "1".equals(d.getJzbfdxbz())).count();

        long xjzc = list.stream().filter(d -> StudentConstants.XJZT_ZC.equals(d.getXjzt())).count();
        long xjxx = list.stream().filter(d -> StudentConstants.XJZT_XX.equals(d.getXjzt())).count();

        long yqzc = list.stream().filter(d -> StudentConstants.STUDENT_DELAY_FALSE.equals(d.getDelayState())).count();
        long yqyq = list.stream().filter(d -> StudentConstants.STUDENT_DELAY_TRUE.equals(d.getDelayState())).count();

        long qrz = list.stream().filter(d -> StudentConstants.XXFS_QRZ.equals(d.getXxfs())).count();
        long fqrz = list.stream().filter(d -> StudentConstants.XXFS_FQRZ.equals(d.getXxfs())).count();

        long qz = list.stream().filter(d -> !StrUtil.isBlank(d.getInten()) && d.getInten().contains("求职")).count();
        long sx = list.stream().filter(d -> !StrUtil.isBlank(d.getInten()) && d.getInten().contains("升学")).count();
        long kg = list.stream().filter(d -> !StrUtil.isBlank(d.getInten()) && d.getInten().contains("考公")).count();
        long ky = list.stream().filter(d -> !StrUtil.isBlank(d.getInten()) && d.getInten().contains("考研")).count();

        List<TableStatistics> all = new ArrayList<>();
        all.add(new TableStatistics(0, "全部学生", list.size()));
        List<TableStatistics> xl = new ArrayList<>();
        xl.add(new TableStatistics(1, "本科", bk));
        xl.add(new TableStatistics(2, "本科二学位", bkrxw));
        xl.add(new TableStatistics(3, "硕士", ss));
        xl.add(new TableStatistics(4, "博士", bs));
        List<TableStatistics> pyfs = new ArrayList<>();
        pyfs.add(new TableStatistics(5, "培养方式定向", dx));
        pyfs.add(new TableStatistics(6, "培养方式委培", wp));
        List<TableStatistics> knslb = new ArrayList<>();
        knslb.add(new TableStatistics(7, "困难生", kns));
        knslb.add(new TableStatistics(8, "精准帮扶对象", jzbfdx));
        List<TableStatistics> xj = new ArrayList<>();
        xj.add(new TableStatistics(9, "学籍状态正常", xjzc));
        xj.add(new TableStatistics(10, "学籍状态休学", xjxx));
        List<TableStatistics> yq = new ArrayList<>();
        yq.add(new TableStatistics(11, "延期状态正常", yqzc));
        yq.add(new TableStatistics(12, "延期状态延期", yqyq));
        List<TableStatistics> xxfs = new ArrayList<>();
        xxfs.add(new TableStatistics(13, "学习方式全日制", qrz));
        xxfs.add(new TableStatistics(14, "学习方式非全日制", fqrz));

        List<String> s = new ArrayList<>();
        s.add("1");
        s.add("2");
        s = s.stream().limit(3).collect(Collectors.toList());

        int id = 15;
        int top = 1;
        List<TableStatistics> qzTable = new ArrayList<>();
        qzTable.add(new TableStatistics(id++, "求职", qz, "inten", ""));
        String intenName = list.stream().filter(d -> !StrUtil.isBlank(d.getIntenName())).map(Student::getIntenName).collect(Collectors.joining(","));
        String[] intenNames = intenName.split(",");
        Map<String, Integer> intenNameMap = new HashMap<String, Integer>();
        for (String x : intenNames) {
            intenNameMap.put(x, intenNameMap.getOrDefault(x, 0) + 1);
        }
        List<Map.Entry<String, Integer>> intenNameList = new ArrayList<Map.Entry<String, Integer>>(intenNameMap.entrySet());
        Collections.sort(intenNameList, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o2.getValue().compareTo(o1.getValue());
            }
        });
        intenNameList = intenNameList.stream().limit(3).collect(Collectors.toList());
        for (Map.Entry<String, Integer> map : intenNameList) {
            qzTable.add(new TableStatistics(id++, "意向职位TOP"+ top++ +"-" + map.getKey(), map.getValue()));
        }

        List<TableStatistics> sxTable = new ArrayList<>();
        sxTable.add(new TableStatistics(id++, "升学(出国出境)", sx, "inten", ""));
        List<Map<String, Object>> sxList = studentIntenService.sxStatistics(yearId, null, null);
        sxList = ListUtil.reverse(sxList).stream().limit(3).collect(Collectors.toList());
        for (Map<String, Object> map : sxList) {
            String name = map.get("name").toString();
            List<QsSchool> qsList = qsSchoolService.list(new LambdaQueryWrapper<QsSchool>().eq(QsSchool::getName, name));
            String qsId = "";
            for (QsSchool qsSchool : qsList) {
                qsId += qsSchool.getId() + ",";
            }
            sxTable.add(new TableStatistics(id++, "意向国家地区TOP"+ top++ +"-" + name, map.get("value"), "sx", qsId));
        }
        top = 1;
        List<Map<String, Object>> sxMajorList = studentIntenService.sxMajorStatistics(yearId, null, null);
        sxMajorList = ListUtil.reverse(sxMajorList).stream().limit(3).collect(Collectors.toList());
        top = 1;
        for (Map<String, Object> map : sxMajorList) {
            String name = map.get("name").toString();
            sxTable.add(new TableStatistics(id++, "意向专业TOP"+ top++ +"-" + name, map.get("value"), "sxMajor", name));
        }

        List<TableStatistics> kgTable = new ArrayList<>();
        kgTable.add(new TableStatistics(id++, "考公", kg, "inten", ""));
        top = 1;
        List<Map<String, Object>> kgList = studentIntenService.kgStatistics(yearId, null, null);
        kgList = ListUtil.reverse(kgList).stream().limit(3).collect(Collectors.toList());
        for (Map<String, Object> map : kgList) {
            String name = map.get("name").toString();
            kgTable.add(new TableStatistics(id++, "意向省市TOP"+ top++ +"-" + name, map.get("value"), "kg", name));
        }
        top = 1;
        List<Map<String, Object>> kgTypeList = studentIntenService.kgTypeStatistics(yearId, null, null);
        kgTypeList = ListUtil.reverse(kgTypeList).stream().limit(3).collect(Collectors.toList());
        for (Map<String, Object> map : kgTypeList) {
            String name = map.get("name").toString();
            kgTable.add(new TableStatistics(id++, "意向类型TOP"+ top++ +"-" + name, map.get("value"), "kgType", name));
        }

        List<TableStatistics> kyTable = new ArrayList<>();
        kyTable.add(new TableStatistics(id++, "考研", ky, "inten", ""));
        top = 1;
        List<Map<String, Object>> kyList = studentIntenService.kyStatistics(yearId, null, null);
        kyList = ListUtil.reverse(kyList).stream().limit(3).collect(Collectors.toList());
        for (Map<String, Object> map : kyList) {
            String name = map.get("name").toString();
            kyTable.add(new TableStatistics(id++, "意向院校TOP"+ top++ +"-" + name, map.get("value"), "ky", name));
        }
        top = 1;
        List<Map<String, Object>> kyMajorList = studentIntenService.kyMajorStatistics(yearId, null, null);
        kyMajorList = ListUtil.reverse(kyMajorList).stream().limit(3).collect(Collectors.toList());
        for (Map<String, Object> map : kyMajorList) {
            String name = map.get("name").toString();
            kyTable.add(new TableStatistics(id++, "意向专业TOP"+ top++ +"-" + name, map.get("value"), "kyMajor", name));
        }

        Map<String, List<TableStatistics>> map = new HashMap<>();
        map.put("all", all);
        map.put("xl", xl);
        map.put("pyfs", pyfs);
        map.put("knslb", knslb);
        map.put("xj", xj);
        map.put("yq", yq);
        map.put("xxfs", xxfs);
        map.put("qzTable", qzTable);
        map.put("sxTable", sxTable);
        map.put("kgTable", kgTable);
        map.put("kyTable", kyTable);

        return JsonResult.ok().setData(map);
    }


    @PreAuthorize("hasAuthority('school:student:list')")
    @ApiOperation(value = "生源大屏")
    @GetMapping("/school/student/screen")
    public JsonResult screen(Integer yearId) {
        List<Student> list = studentService.list(new LambdaQueryWrapper<Student>().eq(Student::getYearId, yearId));

//        long bk = list.stream().filter(d -> StudentConstants.STUDENT_XL_BK.equals(d.getXl())).count();
//        long bkrxw = list.stream().filter(d -> StudentConstants.STUDENT_XL_BKRXW.equals(d.getXl())).count();
//        long ss = list.stream().filter(d -> StudentConstants.STUDENT_XL_SS.equals(d.getXl())).count();
//        long bs = list.stream().filter(d -> StudentConstants.STUDENT_XL_BS.equals(d.getXl())).count();
//        long dx = list.stream().filter(d -> StudentConstants.PYFS_DX.equals(d.getPyfs())).count();
//        long wp = list.stream().filter(d -> StudentConstants.PYFS_WP.equals(d.getPyfs())).count();
//
//        long kns = list.stream().filter(d -> d.getKnslb() != null && !StrUtil.isBlank(d.getKnslb()) && !"0".equals(d.getKnslb())).count();
//        long jzbfdx = list.stream().filter(d -> "1".equals(d.getJzbfdxbz())).count();
//
//        long xjzc = list.stream().filter(d -> StudentConstants.XJZT_ZC.equals(d.getXjzt())).count();
//        long xjxx = list.stream().filter(d -> StudentConstants.XJZT_XX.equals(d.getXjzt())).count();
//
//        long yqzc = list.stream().filter(d -> StudentConstants.STUDENT_DELAY_FALSE.equals(d.getDelayState())).count();
//        long yqyq = list.stream().filter(d -> StudentConstants.STUDENT_DELAY_TRUE.equals(d.getDelayState())).count();
//
//        long qrz = list.stream().filter(d -> StudentConstants.XXFS_QRZ.equals(d.getXxfs())).count();
//        long fqrz = list.stream().filter(d -> StudentConstants.XXFS_FQRZ.equals(d.getXxfs())).count();

        long qz = list.stream().filter(d -> !StrUtil.isBlank(d.getInten()) && d.getInten().contains("求职")).count();
        long sx = list.stream().filter(d -> !StrUtil.isBlank(d.getInten()) && d.getInten().contains("升学")).count();
        long kg = list.stream().filter(d -> !StrUtil.isBlank(d.getInten()) && d.getInten().contains("考公")).count();
        long ky = list.stream().filter(d -> !StrUtil.isBlank(d.getInten()) && d.getInten().contains("考研")).count();

        List<TableStatistics> intenTable = new ArrayList<>();
        intenTable.add(new TableStatistics("求职", qz));
        intenTable.add(new TableStatistics("升学(出国出境)", sx));
        intenTable.add(new TableStatistics("考公", kg));
        intenTable.add(new TableStatistics("考研", ky));

        List<TableStatistics> completeTable = new ArrayList<>();
        long ywc = list.stream().filter(d -> "0".equals(d.getQuestionnaireState())).count();
        long wwc = list.stream().filter(d -> "1".equals(d.getQuestionnaireState())).count();
        completeTable.add(new TableStatistics("已完成", ywc));
        completeTable.add(new TableStatistics("未完成", wwc));

        List<TableStatistics> qzTable = new ArrayList<>();
        String intenName = list.stream().filter(d -> !StrUtil.isBlank(d.getIntenName())).map(Student::getIntenName).collect(Collectors.joining(","));
        String[] intenNames = intenName.split(",");
        Map<String, Integer> intenNameMap = new HashMap<String, Integer>();
        for (String x : intenNames) {
            intenNameMap.put(x, intenNameMap.getOrDefault(x, 0) + 1);
        }
        List<Map.Entry<String, Integer>> intenNameList = new ArrayList<Map.Entry<String, Integer>>(intenNameMap.entrySet());
        Collections.sort(intenNameList, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o2.getValue().compareTo(o1.getValue());
            }
        });
        intenNameList = intenNameList.stream().limit(3).collect(Collectors.toList());
        for (Map.Entry<String, Integer> map : intenNameList) {
            qzTable.add(new TableStatistics(map.getKey(), map.getValue()));
        }

        List<TableStatistics> sxTable = new ArrayList<>();
        List<Map<String, Object>> sxList = studentIntenService.sxStatistics(yearId, null, null);
        sxList = ListUtil.reverse(sxList).stream().limit(3).collect(Collectors.toList());
        for (Map<String, Object> map : sxList) {
            String name = map.get("name").toString();
            List<QsSchool> qsList = qsSchoolService.list(new LambdaQueryWrapper<QsSchool>().eq(QsSchool::getName, name));
            String qsId = "";
            for (QsSchool qsSchool : qsList) {
                qsId += qsSchool.getId() + ",";
            }
            sxTable.add(new TableStatistics(name, Long.parseLong(map.get("value").toString()), qsId));
        }

        List<TableStatistics> sxMajorTable = new ArrayList<>();
        List<Map<String, Object>> sxMajorList = studentIntenService.sxMajorStatistics(yearId, null, null);
        sxMajorList = ListUtil.reverse(sxMajorList).stream().limit(3).collect(Collectors.toList());
        for (Map<String, Object> map : sxMajorList) {
            String name = map.get("name").toString();
            sxMajorTable.add(new TableStatistics(name, Long.parseLong(map.get("value").toString())));
        }

        List<TableStatistics> kgTable = new ArrayList<>();
        List<Map<String, Object>> kgList = studentIntenService.kgStatistics(yearId, null, null);
        kgList = ListUtil.reverse(kgList).stream().limit(3).collect(Collectors.toList());
        for (Map<String, Object> map : kgList) {
            String name = map.get("name").toString();
            kgTable.add(new TableStatistics(name, Long.parseLong(map.get("value").toString())));
        }

        List<TableStatistics> kgTypeTable = new ArrayList<>();
        List<Map<String, Object>> kgTypeList = studentIntenService.kgTypeStatistics(yearId, null, null);
        kgTypeList = ListUtil.reverse(kgTypeList).stream().limit(3).collect(Collectors.toList());
        for (Map<String, Object> map : kgTypeList) {
            String name = map.get("name").toString();
            kgTypeTable.add(new TableStatistics(name, Long.parseLong(map.get("value").toString())));
        }

        List<TableStatistics> kyTable = new ArrayList<>();
        List<Map<String, Object>> kyList = studentIntenService.kyStatistics(yearId, null, null);
        kyList = ListUtil.reverse(kyList).stream().limit(3).collect(Collectors.toList());
        for (Map<String, Object> map : kyList) {
            String name = map.get("name").toString();
            kyTable.add(new TableStatistics(name, Long.parseLong(map.get("value").toString())));
        }

        List<TableStatistics> kyMajorTable = new ArrayList<>();
        List<Map<String, Object>> kyMajorList = studentIntenService.kyMajorStatistics(yearId, null, null);
        kyMajorList = ListUtil.reverse(kyMajorList).stream().limit(3).collect(Collectors.toList());
        for (Map<String, Object> map : kyMajorList) {
            String name = map.get("name").toString();
            kyMajorTable.add(new TableStatistics(name, Long.parseLong(map.get("value").toString())));
        }

        Map<String, Object> map = new HashMap<>();
        map.put("intenTable", intenTable);
        map.put("completeTable", completeTable);
        map.put("qzTable", qzTable);
        map.put("sxTable", sxTable);
        map.put("sxMajorTable", sxMajorTable);
        map.put("kgTable", kgTable);
        map.put("kgTypeTable", kgTypeTable);
        map.put("kyTable", kyTable);
        map.put("kyMajorTable", kyMajorTable);

        List<College> collegeBkList = collegeService.listAll(1, "");
        List<College> collegeYjsList = collegeService.listAll(2, "");

        List<List<Object>> bkTable = new ArrayList<>();
        for (College college : collegeBkList) {
            List<Object> objectList = new ArrayList<>();
            objectList.add(college.getName());
            long qzCount = list.stream().filter(d -> college.getCode().equals(d.getCollegeCode()) && !StrUtil.isBlank(d.getInten()) && d.getInten().contains("求职")).count();
            long sxCount = list.stream().filter(d -> college.getCode().equals(d.getCollegeCode()) && !StrUtil.isBlank(d.getInten()) && d.getInten().contains("升学")).count();
            long kgCount = list.stream().filter(d -> college.getCode().equals(d.getCollegeCode()) && !StrUtil.isBlank(d.getInten()) && d.getInten().contains("考公")).count();
            long kyCount = list.stream().filter(d -> college.getCode().equals(d.getCollegeCode()) && !StrUtil.isBlank(d.getInten()) && d.getInten().contains("考研")).count();
            objectList.add(qzCount);
            objectList.add(sxCount);
            objectList.add(kgCount);
            objectList.add(kyCount);
            bkTable.add(objectList);
        }

        List<List<Object>> yjsTable = new ArrayList<>();
        for (College college : collegeYjsList) {
            List<Object> objectList = new ArrayList<>();
            objectList.add(college.getName());
            long qzCount = list.stream().filter(d -> college.getCode().equals(d.getCollegeCode()) && !StrUtil.isBlank(d.getInten()) && d.getInten().contains("求职")).count();
            long sxCount = list.stream().filter(d -> college.getCode().equals(d.getCollegeCode()) && !StrUtil.isBlank(d.getInten()) && d.getInten().contains("升学")).count();
            long kgCount = list.stream().filter(d -> college.getCode().equals(d.getCollegeCode()) && !StrUtil.isBlank(d.getInten()) && d.getInten().contains("考公")).count();
            objectList.add(qzCount);
            objectList.add(sxCount);
            objectList.add(kgCount);
            yjsTable.add(objectList);
        }

        map.put("bkTable", bkTable);
        map.put("yjsTable", yjsTable);

        return JsonResult.ok().setData(map);
    }


}
